package com.managedBean.merchant;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.FileUploadEvent;

import com.controller.merchant.OffersController;
import com.dto.merchant.OffersDTO;


@ManagedBean(name = "offersManagedBean")
@ViewScoped 
public class OffersManagedBean implements Serializable{


	private static final long serialVersionUID = -3561371048889433802L;
	private String offerName;
	private byte[] bFile;
	@ManagedProperty(value = "#{offersController}")
	private OffersController offersController;
	
	public OffersManagedBean() {
		
	}

	public void handleFileUpload(FileUploadEvent event) {
		bFile = event.getFile().getContents();
	}
	
	public void saveOffer() throws Exception{
		OffersDTO offersDTO=new OffersDTO();
		offersDTO.setOfferImage(bFile);
		offersDTO.setOfferName(offerName);
		offersDTO.setActive(true);
		offersController.saveOrUpdateOffer(offersDTO);
	}

	public void update() {
		OffersDTO offersDTO=new OffersDTO();
		offersDTO.setOfferImage(bFile);
		offersDTO.setOfferName(offerName);
		offersDTO.setActive(true);
		try {
			offersController.updateOffer(offersDTO);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @return the offerName
	 */
	public String getOfferName() {
		return offerName;
	}

	/**
	 * @param offerName the offerName to set
	 */
	public void setOfferName(String offerName) {
		this.offerName = offerName;
	}

	/**
	 * @return the bFile
	 */
	public byte[] getbFile() {
		return bFile;
	}

	/**
	 * @param bFile the bFile to set
	 */
	public void setbFile(byte[] bFile) {
		this.bFile = bFile;
	}

	/**
	 * @return the offersController
	 */
	public OffersController getOffersController() {
		return offersController;
	}

	/**
	 * @param offersController the offersController to set
	 */
	public void setOffersController(OffersController offersController) {
		this.offersController = offersController;
	}
}
